package com.wlanauth.web.core;



import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.json.JSONUtil;

import com.opensymphony.xwork2.ActionSupport;
import com.wlanauth.core.criteria.GeneralCriteria;
import com.wlanauth.domain.manage.Operator;
import com.wlanauth.domain.manage.Role;
import com.wlanauth.domain.manage.SystemResource;
import com.wlanauth.domain.manage.User;
/**
 * ActionSupport基类
 * @author zz
 * @date 2011-5-3
 */
public class BaseActionSupport extends ActionSupport implements
		ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = 1L;
	protected Log logger = LogFactory.getLog(this.getClass());
	
	protected HttpServletRequest request;
	protected HttpServletResponse response;

	protected JsonResult jsonResult;
	
	protected int start;
	protected int limit = 15;
	protected boolean dataOnly = false;
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPageNo(){
		int pageNo = this.start/this.limit+1;
		return pageNo;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	public String actionJsonResult() {
		return this.actionJsonResult(null);
	}
	public String actionJsonResult(JsonResultConfig config) {
		if (this.jsonResult == null) {
			this.jsonResult = new JsonResult(this, "程序错误：未实例化JsonResult对象",false);
		}
		try {
			response.setContentType("text/html;charset=UTF-8"); 
			if(config == null){
				JSONUtil.serialize(response.getWriter(), this.jsonResult);
			} else {
				String str = JSONUtil.serialize(this.jsonResult, 
						config.getExcludePropertyPatterns(), config.getIncludePropertyPatterns(),
						config.isIgnoreHierarchy(), config.isExcludeNullProperties());
				this.response.getWriter().write(str);	
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		} 
		return null;
		//return SystemConstants.RESULT_JSON;
	}
	public String actionJsonResult(Object data){
		try {
			response.setContentType("text/html;charset=UTF-8"); 
			JSONUtil.serialize(response.getWriter(), data);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		} 
		return null;
	}

	public JsonResult getJsonResult() {
		return jsonResult;
	}
	
	public GeneralCriteria buildWebQueryCriteria(Class<?> clazz){
		GeneralCriteria criteria =  new GeneralCriteria(clazz);
		criteria.setLimit(this.limit);
		criteria.setStart(this.start);
		criteria.setDataOnly(this.dataOnly);
		return criteria;
	}

	public boolean isDataOnly() {
		return dataOnly;
	}

	public void setDataOnly(boolean dataOnly) {
		this.dataOnly = dataOnly;
	}
	/**
	 * 取当前操作员
	 * @return
	 */
	public Operator getCurrentOperator(){
		return (Operator) this.request.getSession().getAttribute(SystemConstants.SESSION_OPER_KEY);
	}
	/**
	 * 取当前操作员资源
	 * @return
	 */
	public Set<SystemResource> getCurrentOperatorResources(){
		Operator oper = this.getCurrentOperator();
		if(oper == null){
			throw new SecurityException("未登陆");
		}
		Role role = oper.getRole();
		if(role == null) return null;
		return role.getSystemResources();
	}
	/**
	 * 取得当前用户
	 * @return
	 */
	public User getCurrentUser(){
		User user = (User)this.request.getSession().getAttribute(SystemConstants.SESSION_USER_KEY);
		return user;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}
	
	
}
